<template>
  <div style="margin-top:15px" >
    <!-- <el-form>
      <el-button type="primary" ref="querybtn" @click="chartToShow()">点我看看</el-button>
    </el-form> -->
    <el-select v-model="statisticObj.type" style="margin-bottom:15px" @change="showDataByType(statisticObj.type)" clearable placeholder="请选择">
      <el-option label="近7天" value="sevenDays" />
      <el-option label="近30天" value="aMonth" />
    </el-select>
    <div class="chart-container">
      <div id="chart" class="chart" style="height: 500px; width: 100%" />
    </div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import statisticsApi from "@/api/statistics";
export default {
  name: "topRank",
  data() {
    return {
      chart: null,
      videoLikeData: [],
      statisticObj:{
        type:''
      }
    };
  },
  //设置监听事件
  watch: {
    //videoLikeData获取到值执行chartToShow()事件
    videoLikeData: {
      // deep: true,
      handler: function () {
        // console.log(this);
        this.chartToShow();
      },
    },
  },
  created() {
    // this.videoLikeData();
  },

  mounted() {
    this.getVideoLikeData();
   
  },
  methods: {
    //获取对应类型图表数据 默认为总排行10
    showDataByType(type){
      this.videoLikeData=[]
      statisticsApi.getVideoLikeDataByType(type).then((response)=>{
        for (let item of response.data.videoLikeData) {
          this.videoLikeData.push({ value: item.like, name: item.videoTitle });
        }
        console.log(this.videoLikeData);
      })
    },
    getVideoLikeData() {
      statisticsApi.getVideoLikeData().then((response) => {
        for (let item of response.data.videoLikeData) {
          this.videoLikeData.push({ value: item.like, name: item.videoTitle });
        }
        console.log(this.videoLikeData);
      });
    },
    chartToShow() {
      // 基于准备好的dom，初始化echarts实例
      console.log("chartToShow", this.videoLikeData);
      this.chart = echarts.init(document.getElementById("chart"));
      var option = {
        title: {
          // text: "总播放数视频点赞top10",
          text: this.statisticObj.type == "sevenDays"
              ? "近7天视频点赞top10"
              : this.statisticObj.type == "aMonth"
              ? "近30天视频点赞top10"
              :  "总视频点赞top10",
          //   top:"25",
          left: "63%",
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          left: "left",
        },
        series: [
          {
            type: "pie",
            radius: "70%",
            height: "92%",
            left: "38%",
            data: this.videoLikeData,
            itemStyle: {
              borderColor: "#fff",
              borderWidth: 1,
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };

      this.chart.setOption(option);
    },
  },
};
</script>

<style scoped>
</style>
